package com.yicun.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yicun.companyapplication.R;
import com.yicun.model.Company;
import com.yicun.utils.DateUtils;
import com.yicun.utils.StringUtils;

import java.util.List;

public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.CompanyViewHolder> {
    private List<Company> companyList;
    private OnItemClickListener onItemClickListener;
    private OnStarClickListener onStarClickListener;

    public interface OnItemClickListener {
        void onItemClick(Company company);
    }

    public interface OnStarClickListener {
        void onStarClick(Company company, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnStarClickListener(OnStarClickListener listener) {
        this.onStarClickListener = listener;
    }

    public CompanyAdapter(List<Company> companyList) {
        this.companyList = companyList;
    }

    @NonNull
    @Override
    public CompanyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_company, parent, false);
        return new CompanyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CompanyViewHolder holder, int position) {
        Company company = companyList.get(position);

        // 基础信息
        holder.tvCompanyName.setText(company.getCompanyName());
        holder.tvCompanyType.setText(String.format("%s | %s", company.getCompanyType(), company.getIndustry()));

        // 状态和资本信息
        String statusText = String.format("状态: %s | 注册资本: %s万 | 参保人数: %s人" ,
                company.getBusinessStatus(),
                StringUtils.formatCapital(company.getRegisteredCapital()),company.getInsuredCount());
        holder.tvStatusCapital.setText(statusText);

        // 联系信息
        String contactInfo = String.format("电话: %s | 更多电话: %d个",
                company.getPhone(),
                company.getMorePhones() != null ? company.getMorePhones().size() : 0);
        holder.tvContactInfo.setText(contactInfo);

        // 收藏状态
        holder.ivStar.setImageResource(
                "1".equals(company.getIsStar()) ?
                        R.drawable.ic_star_filled : R.drawable.ic_star_outline);

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(company);
            }
        });

        holder.ivStar.setOnClickListener(v -> {
            if (onStarClickListener != null) {
                onStarClickListener.onStarClick(company, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return companyList != null ? companyList.size() : 0;
    }

    public void updateData(List<Company> newCompanies) {
        if (newCompanies != null) {
            companyList = newCompanies;
            notifyDataSetChanged();
        }
    }

    public void toggleStar(int position) {
        if (position >= 0 && position < companyList.size()) {
            Company company = companyList.get(position);
            company.setIsStar("1".equals(company.getIsStar()) ? "0" : "1");
            notifyItemChanged(position);
        }
    }

    static class CompanyViewHolder extends RecyclerView.ViewHolder {
        TextView tvCompanyName, tvCompanyType, tvStatusCapital, tvContactInfo;
        ImageView ivStar;

        public CompanyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCompanyName = itemView.findViewById(R.id.tv_company_name);
            tvCompanyType = itemView.findViewById(R.id.tv_company_type);
            tvStatusCapital = itemView.findViewById(R.id.tv_status_capital);
            tvContactInfo = itemView.findViewById(R.id.tv_contact_info);
            ivStar = itemView.findViewById(R.id.iv_star);
        }
    }
}